<?php

namespace app\platform\controller\admin;

use app\beautiful\model\member\MemberModel;
use app\BaseController;
use app\stores\model\promotion\PromotionmemberModel;
use app\stores\model\promotion\TeamModel;


class Promotionteam extends BaseController{

    public function list(){
        $limit=$this->request->limit;
        $page=$this->request->page;
        $name= $this->request->param('name');
        $mobile = $this->request->param('mobile');
        $member_num = $this->request->param('member_num');
        $where=[];
        $order['a.id']='desc';
        if(!empty($name)){
            $where['name']=$name;
        }
        if(!empty($mobile)){
            $where['mobile']=$mobile;
        }
        if(!empty($member_num)){
            $where['member_num']=$member_num;
        }
        $res_data=TeamModel::alist($where,$order,$page,$limit);
        return $this->success($res_data);
    }
    public function info(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $where['id']=$id;
        $res_data=TeamModel::info($where);
        return $this->success($res_data);
    }
    public function effect(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_effect=$this->request->param('is_effect',null);
        if($is_effect===null){
            return $this->error('is_effect参数错误');
        }
        $where['id']=$id;
        $up['is_effect']=$is_effect;
        $res=TeamModel::up($up,$where);
        return $this->success();
    }
    public function del(){
        $id = $this->request->param('id');
        if(empty($id)){
            return $this->error('ID错误');
        }
        $is_delete=1;
        $where['id']=$id;
        $info=TeamModel::info($where);
        if(empty($info)){
            return $this->error('数据不存在');
        }
        $up['is_delete']=$is_delete;
        $res=TeamModel::up($up,$where);
        if(!$res){
            return $this->error('删除失败');
        }
        return $this->success();
    }
    public function edit(){
        $id = $this->request->param('id',0);
        $name = $this->request->param('name',null);
        $remark=$this->request->param('remark',null);
        $imgurl=$this->request->param('imgurl',null);
        $member_id=$this->request->param('member_id',null);
        $username=$this->request->param('username',null);
        $mobile=$this->request->param('mobile',null);
        if($id==0){
            return $this->error('id错误');
        }
        $TeamModel= new TeamModel();
        $all_info=$TeamModel->where(['id'=>$id])->find();
        if(empty($all_info)) return $this->error('数据不存在');
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['id','<>',$id];
        $s[]=['is_delete','=',0];
        $info=$TeamModel->where($s)->find();
        if(!empty($info)) return $this->error('数据重复');
        $up=[];
        if($name!=null) $up['name']=$name;
        if($remark!=null) $up['remark']=$remark;
        if($imgurl!=null) $up['imgurl']=$imgurl;
        if($member_id!=null) $up['member_id']=$member_id;
        if($username!=null) $up['username']=$username;
        if($mobile!=null) $up['mobile']=$mobile;
        $where['id']=$id;
        $res_data=TeamModel::up($up,$where);
        if (!empty($up['member_id']) && $up['member_id']!=$all_info['member_id']){
            $member_info=MemberModel::where(['member_id'=>$member_id])->find()->toArray();
            $p_up['member_id']=$member_id;
            $p_up['name']=$username;
            $p_up['sex']=$member_info['sex'];
            $p_up['mobile']=$mobile;
            $p_s=[];
            $p_s['team_id']=$id;
            $p_s['type']=2;
            PromotionmemberModel::up($p_up,$p_s);
            //添加团队信息
            $p_insert['add_time']=date('Y-m-d H:i:s');
            $p_insert['edit_time']=date('Y-m-d H:i:s');
            $p_insert['member_id']=$member_id;
            $p_insert['type']=1;
            $p_insert['name']=$username;
            $p_insert['sex']=$member_info['sex'];
            $p_insert['mobile']=$mobile;
            $p_insert['team_id']=$id;
            PromotionmemberModel::insertGetId($p_insert);
            $old_member_id=$all_info['member_id'];
            $d_s=[];
            $d_s['member_id']=$old_member_id;
            $d_s['team_id']=$id;
            $d_s['type']=1;
            PromotionmemberModel::where($d_s)->delete();
        }
        return $this->success($res_data);
    }
    public function add(){
        $name = $this->request->param('name',null);
        $remark=$this->request->param('remark',null);
        $imgurl=$this->request->param('imgurl',null);
        $member_id=$this->request->param('member_id',null);
        $username=$this->request->param('username',null);
        $mobile=$this->request->param('mobile',null);
        $TeamModel= new TeamModel();
        $s=[];
        $s[]=['name','=',$name];
        $s[]=['is_delete','=',0];
        $all_info=$TeamModel->where($s)->find();
        if(!empty($all_info)) return $this->error('数据重复');
        $team_num=$TeamModel->team_num();
        $insert=compact('name','remark','imgurl','member_id','username','mobile','team_num');
        $insert['add_time']=date('Y-m-d H:i:s');
        $insert['edit_time']=date('Y-m-d H:i:s');
        $insert_id=TeamModel::insertGetId($insert);
        $member_info=MemberModel::where(['member_id'=>$member_id])->find();
        $p_insert['add_time']=date('Y-m-d H:i:s');
        $p_insert['edit_time']=date('Y-m-d H:i:s');
        $p_insert['member_id']=$member_id;
        $p_insert['type']=2;
        $p_insert['name']=$username;
        $p_insert['sex']=$member_info['sex'];
        $p_insert['mobile']=$mobile;
        $p_insert['team_id']=$insert_id;
        PromotionmemberModel::insertGetId($p_insert);
        $p_insert['type']=1;
        PromotionmemberModel::insertGetId($p_insert);
        return $this->success($insert_id);
    }

}

